package com.cqw.commontest.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-06-01 16:39
 * @Description: map  flatMap
 * 映射：可以将一个流的元素按照一定的映射规则映射到另一个流中，分为map和flatMap
 * 1、map：接收一个函数作为参数，该函数会被用到每个元素上，并将其映射成一个新的元素
 * 2.flatMap：接收一个函数作为参数，将流中的每个值转成另一个流，然后把所有流连成一个流
 */
public class StreamTest3 {


    public static void main(String[] args) {

        //英文字符串数组元素全部转大写字母
        List<String> strings = Arrays.asList("ertert", "w4sdfsd", "dfgewt");
        List<String> collect = strings.stream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(collect);


        //每个元素加3
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 7, 8);
        List<Integer> collect1 = list.stream().map(t -> t + 3).collect(Collectors.toList());


        //将员工工资加1000
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 1000, 21, "男", "上海"));
        personList.add(new Person("李四", 2000, 25, "女", "安徽"));
        personList.add(new Person("王五", 2200, 36, "男", "杭州"));
        personList.add(new Person("赵六", 1400, 25, "女", "南京"));
        personList.add(new Person("曹七", 3000, 12, "男", "北京"));
        personList.add(new Person("Lily", 5000, 42, "女", "上海"));

        List<Person> collect2 = personList.stream().map(t -> {
            t.setSalary(t.getSalary() + 1000);      //会改变原来的值，不想改变的话  new 一个对象
            return t;
        }).collect(Collectors.toList());
        System.out.println(collect2);
        System.out.println(personList);


        //将讲个元素合并
        List<String> strings1 = Arrays.asList("m-e-g-t", "21-4-3");
        List<String> collect3 = strings1.stream().flatMap(t -> {
            String[] split = t.split("-");
            return Arrays.stream(split);
        }).collect(Collectors.toList());
        System.out.println(collect3);   //[m, e, g, t, 21, 4, 3]


    }


}
